package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.entity.Course;
import com.edu.entity.Teacher;

import com.edu.entity.course.CourseFontVo;
import com.edu.mapper.CourseMapper;
import com.edu.mapper.TeacherMapper;
import com.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author mxs
 * @since 2021-10-13
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private CourseMapper courseMapper;

    @Cacheable(key = "'selectTeacherList'",value = "teacher")
    @Override
    public List<Teacher> getHotTeacher() {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 4");
        List<Teacher> teachers = baseMapper.selectList(queryWrapper);
        System.out.println("进入了数据库查询teacher");
        return teachers;
    }

    @Override
    public HashMap<String, Object> getCoursePage(Page<Course> coursePage, CourseFontVo teacherFontVo) {
        //获取视图类中的信息
        String buyCountSort = teacherFontVo.getBuyCountSort();
        String gmtCreateSort = teacherFontVo.getGmtCreateSort();
        String priceSort = teacherFontVo.getPriceSort();
        String subjectId = teacherFontVo.getSubjectId();
        String subjectParentId = teacherFontVo.getSubjectParentId();
        String title = teacherFontVo.getTitle();
        String teacherId = teacherFontVo.getTeacherId();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //判断视图类中的信息是否为空，如果不为空就添加上去
        //排序部分
        if (!StringUtils.isEmpty(buyCountSort)){
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(gmtCreateSort)){
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(priceSort)){
            queryWrapper.orderByDesc("price");
        }
        //准确查值部分
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(title)){
            queryWrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacherId",teacherId);
        }

        //分页查询
        courseMapper.selectPage(coursePage, queryWrapper);

        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        long size = coursePage.getSize();
        long current = coursePage.getCurrent();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        long pages = coursePage.getPages();

        HashMap<String, Object> hashMap = new HashMap<>();
        //将数据放到map中
        hashMap.put("total",total);
        hashMap.put("items",records);
        hashMap.put("size",size);
        hashMap.put("current",current);
        hashMap.put("hasNext",hasNext);
        hashMap.put("hasPrevious",hasPrevious);
        hashMap.put("pages",pages);

        return hashMap;


    }

    @Override
    public HashMap<String, Object> getTeacherFontList(Page<Teacher> teacherPage) {

        //根据id降序排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        baseMapper.selectPage(teacherPage,queryWrapper);

        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        long size = teacherPage.getSize();
        long current = teacherPage.getCurrent();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();
        long pages = teacherPage.getPages();

        HashMap<String, Object> hashMap = new HashMap<>();
        //将数据放到map中
        hashMap.put("total",total);
        hashMap.put("items",records);
        hashMap.put("size",size);
        hashMap.put("current",current);
        hashMap.put("hasNext",hasNext);
        hashMap.put("hasPrevious",hasPrevious);
        hashMap.put("pages",pages);

        return hashMap;
    }
}
